<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Process;

use App\Services\SubscribeHandleService;
use Hyperf\Process\AbstractProcess;
use Hyperf\Process\Annotation\Process;

/**
 * Websocket 消息订阅处理服务
 */
#[Process(name: 'Redis WebSocket 消息订阅')]
class RedisWebsocketSubscribe extends AbstractProcess
{
    /**
     * 订阅的通道.
     *
     * @var string[]
     */
    private $chans = [
        'websocket',
    ];

    private SubscribeHandleService $handleService;

    /**
     * 执行入口.
     */
    public function handle(): void
    {
        $this->handleService = di()->get(SubscribeHandleService::class);
        redis()->subscribe($this->chans, [$this, 'subscribe']);
    }

    /**
     * 订阅处理逻辑.
     * @param mixed $redis
     */
    public function subscribe($redis, string $chan, string $message)
    {
        $data = json_decode($message, true);
        logInfo('RedisWebsocketSubscribe', 'handle', json_encode($data));
        $this->handleService->handle($data);
    }
}
